﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DTC.Database;

namespace DTC.Popups
{
    /// <summary>
    /// Interaction logic for SupplierPopup.xaml
    /// </summary>
    public partial class SupplierPopup : Window
    {
        public SupplierPopup()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            supplier = new DTCSupplier();
            MainGrid.DataContext = supplier;
        }

        private DTCSupplier supplier;
        public DTCSupplier Supplier
        {
            get
            {
                return supplier;
            }
            set
            {
                if (supplier != value)
                {
                    supplier = value;
                    MainGrid.DataContext = supplier;
                }
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.DialogResult == true)
            {
                SupplierNameTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                SupplierAddressTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                SupplierPhoneTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                List<string> errList = new List<string>();
                string message = "";
                if (new DTC.Validations.FormValidate().FormHasError(errList, MainGrid))
                {
                    foreach (string str in errList)
                        //if(!(message.Contains(str)))
                        message += str;
                    WarningPopup wp = new WarningPopup();
                    wp.Owner = this;
                    wp.TitleText = "Недостаточно данных";
                    wp.Message = message;
                    wp.ShowDialog();
                    e.Cancel = true;
                    return;
                }
                if (supplier.SupplierID == 0)
                    supplier.Insert();
                else
                    supplier.Update();
            }
            else
                supplier.Cancel();
        }
    }
}
